package cm.iccsoft.android.telecomcost.activities;

import java.util.ArrayList;
import com.google.gson.Gson;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.Utility;
import cm.iccsoft.android.telecomcost.adapter.ProviderAdapter;
import cm.iccsoft.android.telecomcost.entities.Country;
import cm.iccsoft.android.telecomcost.entities.Provider;
import cm.iccsoft.android.telecomcost.ui.ProviderDialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;

public class ProviderListActivity extends TelecomcostActivity {

	private ListView provider_list;
	private DatabaseManager db;
	private ArrayList<Provider> providers;

	public static final int DIALOG_NEW_PROVIDER = 1;
	public static final int DIALOG_EDIT_PROVIDER = 2;

	public Dialog onCreateDialog(int id) {
		Log.i("ProviderListActivity", "Creating dialog ohne bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_NEW_PROVIDER:
			dialog = new ProviderDialog(ProviderListActivity.this, null);
			break;
		default:
			dialog = null;
		}

		return dialog;
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		Log.i("ProviderListActivity", "Creating dialog with bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_EDIT_PROVIDER:
			if (args.getString(Provider.PROP_TYPE) != null) {
				Gson gson = TelecomCost.getGson();
				Provider provider = gson.fromJson(
						args.getString(Provider.PROP_TYPE), Provider.class);
				Log.i("ProviderListActivity",
						"Creating dialog with bundle to edit the provider "
								+ provider.getName());
				dialog = new ProviderDialog(ProviderListActivity.this, provider);
			} else {
				Log.i("ProviderListActivity",
						"cannot display the dialog for editing...no provider selected");
			}
			break;
		default:
			dialog = null;
		}

		if (dialog == null) {
			return onCreateDialog(id);
		}

		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		Log.i("ProviderListActivity", "Preparing dialog with bundle");
		super.onPrepareDialog(id, dialog, args);
		if (args != null) {
			ProviderDialog _dialog = (ProviderDialog) dialog;
			Provider provider = _dialog.getProvider();

			if (provider == null && args.containsKey(Provider.PROP_TYPE)) {
				Gson gson = TelecomCost.getGson();
				_dialog.setProvider(gson.fromJson(
						args.getString(Provider.PROP_TYPE), Provider.class));
			}
		}
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		Log.i("ProviderListActivity", "Preparing dialog ohne bundle");
		super.onPrepareDialog(id, dialog);
		Utility.setDialogLayout(dialog, LayoutParams.WRAP_CONTENT, 99);
	}

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("ProviderListActivity", "Creating the activity");

		super.onCreate(savedInstanceState);
		db = TelecomCost.getDatabaseManager();

		setContentView(R.layout.provider_list);

		provider_list = (ListView) findViewById(R.id.listViewProvider);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("ProviderListActivity", "creating options menu for providers : ");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_providers, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.i("ProviderListActivity", "onOptionsItemSelected");
		switch (item.getItemId()) {
		case R.id.menu_item_new_provider:
			showDialog(DIALOG_NEW_PROVIDER);
			return true;
		case R.id.menu_item_delete_providers:
			deleteProvider();
			bindProvider();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	protected void deleteProvider() {
		int i = 0;
		Gson gson = TelecomCost.getGson();

		while (i < providers.size()) {
			Provider provider = providers.get(i);
			if (provider.isSelected()) {
				Country country = gson.fromJson(
						db.getData(Country.PROP_TYPE, provider.getCountryId()),
						Country.class);
				if (country != null) {
					Log.i("ProviderListActivity",country.remove(provider) ?
							"provider removed..." : "could not remove provider from country " + country.getName());					
				} else {
					Log.i("ProviderListActivity",
							"could not load the country of the provider "
									+ provider.getName());
				}
				Log.i("ProviderListActivity", "Deleting selected provider : ");
				db.deleteData(Provider.PROP_TYPE, providers.get(i).getId());
			}
			i++;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i("ProviderListActivity", "onActivityResult");
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		Log.i("ProviderListActivity", "Destroying the activity");
		super.onDestroy();
		provider_list = null;
	}

	@Override
	protected void onPause() {
		Log.i("ProviderListActivity", "Pausing the activity");
		super.onPause();
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		Log.i("ProviderListActivity", "Postcreating the activity");
		super.onPostCreate(savedInstanceState);
	}

	@Override
	protected void onPostResume() {
		Log.i("ProviderListActivity", "PostResuming the activity");
		super.onPostResume();
		bindProvider();
	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Log.i("ProviderListActivity",
				"activity has been attached to the window manager");
	}

	@Override
	protected void onRestart() {
		Log.i("ProviderListActivity", "Restarting the activity");
		super.onRestart();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.i("ProviderListActivity", "onRestoreInstanceState the activity");
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		Log.i("ProviderListActivity", "Resuming the activity");
		super.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.i("ProviderListActivity", "onSaveInstanceState the activity");
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onStart() {
		Log.i("ProviderListActivity", "Starting the activity");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.i("ProviderListActivity", "Stopping the activity");
		super.onStop();
	}

	public void bindProvider() {
		Log.i("ProviderListActivity", "binding provider...");
		providers = db.getProviders();

		provider_list.setAdapter(new ProviderAdapter(ProviderListActivity.this,
				R.layout.provider_view, providers, this));
	}

	@Override
	public String toString() {
		return "ProviderListActivity";
	}

	public Provider getProvider(String name) {
		String _name;
		int i = 0;
		while (i < providers.size()) {
			_name = providers.get(i).getName();
			if (_name.equals(name)) {
				Log.i("ProviderListActivity",
						"this provider already exists...we update it");
				return providers.get(i);
			}
			i++;
		}

		return null;
	}
}
